package com.EM1.www.servlet;
import com.EM1.www.constant.constant;
import com.EM1.www.dao.ConnectIdentity;
import com.EM1.www.dao.ConnectTeacher;
import com.EM1.www.dao.ConnectUser;
import com.EM1.www.po.User;
import com.EM1.www.service.CheckUSer;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;

@WebServlet("/Registe")
public class Registe extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String okpassword = request.getParameter("okpassword");
        String isteacher = request.getParameter("isteacher");
        String checkcode=request.getParameter("checkcode");
        String warn=null;
        HttpSession session = request.getSession();
        String rightcheckcode = (String) session.getAttribute("checkcode");
        if(!rightcheckcode.equals(checkcode)){
            warn="验证码错误";
        }
        boolean isteacherb = Boolean.parseBoolean(isteacher);
        if(isteacherb){
            String identify = request.getParameter("identify");
            ConnectIdentity connectIdentity=new ConnectIdentity();
            try {
                int id = connectIdentity.Selectidentify(identify);
                if(id==-1){
                    warn="身份码错误";
                }else {
                    connectIdentity.Deleteidentify(id);
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        CheckUSer checkUser=new CheckUSer();
        try {
            //数据库操作
            warn= checkUser.okUser(username,password,okpassword);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        System.out.println(username);
        if(warn!=null){
            request.setAttribute("warn",warn);
            request.getRequestDispatcher("/registe.jsp").forward(request,response);
        }else {
            ConnectUser connectUser=new ConnectUser();
            ConnectTeacher connectTeacher=new ConnectTeacher();
            try {
                connectUser.setUSer(username,password,isteacherb);
                User user = connectUser.selectusername(username);
                connectTeacher.SetTeacher(username,user.getId());
            }catch (Exception e) {
                throw new RuntimeException(e);
            }
            Cookie cookie=new Cookie("username",username);
            response.addCookie(cookie);
            request.getRequestDispatcher("/login.jsp").forward(request,response);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
